//
//  FindCar.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/25.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "FindCar.h"
#import <AVObject.h>
#import <AVRelation.h>
#import <SVProgressHUD.h>

#import "JTConst.h"
#import "JTPassengerTrip.h"
#import "JTUser.h"

@interface FindCar ()

@property (nonatomic, strong) AVUser * currentUser;


@end

@implementation FindCar

+ (void)passenger:(AVObject *)object find:(JTPassengerTrip *)trip {
    [self passenger:object find:trip same:nil];
}

+ (void)passenger:(AVObject *)object find:(JTPassengerTrip *)trip same:(JTPassengerTrip *)sameTrip {
    AVObject * onePassenger = [AVObject objectWithoutDataWithClassName:kJTClassPassenger objectId:object.objectId];
    
    if (sameTrip) {
        AVObject *samePassengerTrip = [AVObject objectWithoutDataWithClassName:kJTClassJourneyPassenger objectId:sameTrip.objectId];
    }
    
    AVObject *passengerTrip = [AVObject objectWithClassName:kJTClassJourneyPassenger];
    [passengerTrip setObject:trip.startLocation forKey:kJTJourneyKeyStart];
    [passengerTrip setObject:trip.startStreet forKey:kJTJourneyKeyStartStreet];
    [passengerTrip setObject:trip.endLocation forKey:kJTJourneyKeyEnd];
    [passengerTrip setObject:trip.endStreet forKey:kJTJourneyKeyEndStreet];
    [passengerTrip setObject:trip.time forKey:kJTJourneyKeyDate];
    [passengerTrip setObject:trip.timeInterval forKey:kJTJourneyKeyTimeInterval];
    [passengerTrip setObject:trip.remark forKey:kJTJourneyKeyRemark];
    [passengerTrip setObject:trip.orderSeatNumber forKey:kJTJourneyKeySeats];
    [passengerTrip setObject:trip.identity forKey:kJTJourneyKeyIdentity];
    [passengerTrip setObject:trip.passenger forKey:@"PassengerInformation"];
    [passengerTrip setObject:trip.price forKey:@"Price"];
    [passengerTrip setObject:trip.start forKey:@"Start"];
    [passengerTrip setObject:trip.end forKey:@"End"];
    AVObject * user =[AVObject objectWithoutDataWithClassName:@"_User" objectId:[AVUser currentUser].objectId];
    [user incrementKey:@"current"];
    [user save];
    [passengerTrip saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[NSUserDefaults standardUserDefaults] setObject:passengerTrip.objectId forKey:tmpPassTripID];
            [[NSUserDefaults standardUserDefaults] setObject:trip.orderSeatNumber forKey:tmpPassTripSeats];
            
            AVRelation *driverTripInfo = [onePassenger relationForKey:kJTPassengerKeyInfo];
            [driverTripInfo addObject:passengerTrip];
            
            [onePassenger saveInBackground];
        }
    }];
}

+ (void)firstFind:(JTPassengerTrip *)trip {
    AVObject * passenger = [AVObject objectWithClassName:kJTClassPassenger];
    [passenger setObject:trip.passenger.objectId forKey:kJTPassengerKeyId];
    
    AVObject *passengerTrip = [AVObject objectWithClassName:kJTClassJourneyPassenger];
    [passengerTrip setObject:trip.startLocation forKey:kJTJourneyKeyStart];
    [passengerTrip setObject:trip.startStreet forKey:kJTJourneyKeyStartStreet];
    [passengerTrip setObject:trip.endLocation forKey:kJTJourneyKeyEnd];
    [passengerTrip setObject:trip.endStreet forKey:kJTJourneyKeyEndStreet];
    [passengerTrip setObject:trip.time forKey:kJTJourneyKeyDate];
    [passengerTrip setObject:trip.timeInterval forKey:kJTJourneyKeyTimeInterval];
    [passengerTrip setObject:trip.remark forKey:kJTJourneyKeyRemark];
    [passengerTrip setObject:trip.orderSeatNumber forKey:kJTJourneyKeySeats];
    [passengerTrip setObject:trip.identity forKey:kJTJourneyKeyIdentity];
    [passengerTrip setObject:trip.passenger forKey:@"PassengerInformation"];
    [passengerTrip setObject:trip.price forKey:@"Price"];
    [passengerTrip setObject:trip.start forKey:@"Start"];
    [passengerTrip setObject:trip.end forKey:@"End"];
    AVObject * user =[AVObject objectWithoutDataWithClassName:@"_User" objectId:[AVUser currentUser].objectId];
    [user incrementKey:@"current"];
    [user save];
    [passengerTrip saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[NSUserDefaults standardUserDefaults] setObject:passengerTrip.objectId forKey:tmpPassTripID];
            [[NSUserDefaults standardUserDefaults] setObject:trip.orderSeatNumber forKey:tmpPassTripSeats];
            
            AVRelation *driverTripInfo = [passenger relationForKey:kJTPassengerKeyInfo];
            [driverTripInfo addObject:passengerTrip];
            
            [passenger saveInBackground];
        }
    }];
    
}


-(void)saveInCloud:(NSString *)objectId start:(NSString *)startLocation end:(NSString *)endLocation number:(NSNumber *)number time:(NSString *)time remark:(NSString *)mark current:(AVUser *)currentUser
{

    AVObject * passenger = [AVObject objectWithoutDataWithClassName:@"PassengerInfo" objectId:objectId];
    
    AVObject * passengerTrip =[AVObject objectWithClassName:@"PassengerTrip"];
    
    AVRelation * passengerTripInfo = [passenger relationForKey:@"PassengerTripInfo"];
    
    [passengerTrip setObject:startLocation forKey:@"StartLocation"];
    
    [passengerTrip setObject:endLocation forKey:@"EndLocation"];
    
    [passengerTrip setObject:time forKey:@"Time"];
    
    [passengerTrip setObject:number forKey:@"NumberOfOrderSeat"];
    
    [passengerTrip setObject:mark forKey:@"Remark"];
    
    [passengerTrip setObject:currentUser forKey:@"PassengerInformation"];
    
    [passengerTrip setObject:@"乘客" forKey:@"Identity"];
    
    [passengerTrip save];
    
    [passengerTripInfo addObject:passengerTrip];
    
    [passenger saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            [[NSUserDefaults standardUserDefaults] setObject:passengerTrip.objectId forKey:@"TripObjectId"];
            
        [SVProgressHUD showSuccessWithStatus:@"订单发送成功!"];
        }
        else
        {
            NSLog(@"%@",error);
            
            [SVProgressHUD showErrorWithStatus:@"发送失败！"];
        }
    }];

}

-(void)otherSaveInCloud:(NSString *)objectId start:(NSString *)startLocation end:(NSString *)endLocation number:(NSNumber *)number time:(NSString *)time remark:(NSString *)mark current:(AVUser *)currentUser

{
    AVObject * passenger = [AVObject objectWithClassName:@"PassengerInfo"];
    
    [passenger setObject:objectId forKey:@"PassengerId"];
    
    [passenger setObject:_currentUser forKey:@"PassengerPointer"];
    
    AVObject * passengerTrip =[AVObject objectWithClassName:@"PassengerTrip"];
    
    AVRelation * passengerTripInfo = [passenger relationForKey:@"PassengerTripInfo"];
    
    [passengerTrip setObject:startLocation forKey:@"StartLocation"];
    
    [passengerTrip setObject:endLocation forKey:@"EndLocation"];
    
    [passengerTrip setObject:time forKey:@"Time"];
    
    [passengerTrip setObject:number forKey:@"NumberOfOrderSeat"];
    
    [passengerTrip setObject:mark forKey:@"Remark"];
    
    [passengerTrip setObject:currentUser forKey:@"PassengerInformation"];
    
    [passengerTrip setObject:@"乘客" forKey:@"Identity"];
    
    [passengerTrip save];
    
    [passengerTripInfo addObject:passengerTrip];
    
    [passenger saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            [[NSUserDefaults standardUserDefaults] setObject:passenger.objectId forKey:@"TripObjectId"];
            
            [SVProgressHUD showSuccessWithStatus:@"订单发送成功!"];
            
        }
        else
        {
        
            
            
        }
    }];
    
}


//+ (void)driver:(AVObject *)object publish:(JTDriverTrip *)trip
//{
//
//}
@end
